package com.gaattc.controller.admin;

import com.gaattc.model.GoodsSpecs;
import com.gaattc.model.Result;
import com.gaattc.model.bo.AddGoodsBO;
import com.gaattc.model.bo.AddTypeBO;
import com.gaattc.model.bo.UpdateGoodsBO;
import com.gaattc.model.vo.GetGoodsByTypeVO;
import com.gaattc.model.vo.GetGoodsInfoVO;
import com.gaattc.model.vo.GoodsTypeVO;
import com.gaattc.model.vo.MsgVO;
import com.gaattc.service.GoodsService;
import com.gaattc.service.GoodsServiceImpl;
import com.gaattc.utils.FileUploadUtils;
import com.gaattc.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@WebServlet("/api/admin/goods/*")
public class GoodsServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImpl();

    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/goods/", "");
        if("addType".equals(action)) {
            addType(request, response);
        } else if("imgUpload".equals(action)) {
            imgUpload(request, response);
        } else if("addGoods".equals(action)) {
            addGoods(request, response);
        } else if("updateGoods".equals(action)) {
            updateGoods(request, response);
        } else if("addSpec".equals(action)) {
            addSpec(request, response);
        } else  if("deleteSpec".equals(action)) {
            deleteSpec(request, response);
        } else if("reply".equals(action)) {
            reply(request, response);
        }
    }

    /**
     * 回复留言
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void reply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        MsgVO msgVO = null;
        try {
            msgVO = gson.fromJson(requestBody, MsgVO.class);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(gson.toJson(Result.error("参数不合法")));
        }
        goodsService.reply(msgVO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 删除一个商品规格
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void deleteSpec(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsSpecs goodsSpecs = null;
        try {
            goodsSpecs = gson.fromJson(requestBody, GoodsSpecs.class);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(gson.toJson(Result.error("参数不合法，请检查")));
            return;
        }
        goodsService.deleteSpec(goodsSpecs);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 添加一个商品规格
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void addSpec(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsSpecs goodsSpecs = null;
        try {
            goodsSpecs = gson.fromJson(requestBody, GoodsSpecs.class);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(gson.toJson(Result.error("参数不合法，请检查")));
            return;
        }
        goodsService.addSpec(goodsSpecs);
        response.getWriter().println(gson.toJson(Result.ok(goodsSpecs)));
    }

    /**
     * 更新商品信息
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void updateGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        UpdateGoodsBO updateGoodsBO = null;
        try{
            updateGoodsBO = gson.fromJson(requestBody, UpdateGoodsBO.class);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(gson.toJson(Result.error("参数不合法，请检查")));
            return;
        }
        goodsService.updateGoods(updateGoodsBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 添加商品
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AddGoodsBO addGoodsBO = null;
        try {
            addGoodsBO = gson.fromJson(requestBody, AddGoodsBO.class);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(gson.toJson(Result.error("参数不合法，请检查")));
            return;
        }
        goodsService.addGoods(addGoodsBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 上传图片
     * @param request req
     * @param response resp
     */
    private void imgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> uploadMap = FileUploadUtils.parseRequest(request);
        String file = (String) uploadMap.get("file");
        String domain = (String) getServletContext().getAttribute("domain");
        response.getWriter().println(gson.toJson(Result.ok(domain + "/" + file)));
    }

    /**
     * 添加商品类型
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void addType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AddTypeBO addTypeBO = gson.fromJson(requestBody, AddTypeBO.class);
        int code = goodsService.addType(addTypeBO);
        if(code == 1) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("添加失败")));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/goods/", "");
        if("getType".equals(action)) {
            getType(request, response);
        } else if("getGoodsByType".equals(action)) {
            getGoodsByType(request, response);
        } else if("deleteType".equals(action)) {
            deleteType(request, response);
        } else if("deleteGoods".equals(action)) {
            deleteGoods(request, response);
        } else if("getGoodsInfo".equals(action)) {
            getGoodsInfo(request, response);
        } else if ("noReplyMsg".equals(action)) {
            noReplyMsg(request, response);
        } else if("repliedMsg".equals(action)) {
            repliedMsg(request, response);
        }
    }

    /**
     * 获取已回复留言
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void repliedMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<MsgVO> msg = goodsService.msg(0);
        response.getWriter().println(gson.toJson(Result.ok(msg)));
    }

    /**
     * 获取未回复留言
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void noReplyMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<MsgVO> msg = goodsService.msg(1);
        response.getWriter().println(gson.toJson(Result.ok(msg)));
    }

    /**
     * 编辑时获取商品信息
     * @param request req
     * @param response resp
     */
    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        GetGoodsInfoVO getGoodsInfoVO = goodsService.getGoodsInfo(id);
        response.getWriter().println(gson.toJson(Result.ok(getGoodsInfoVO)));
    }

    /**
     * 删除商品
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void deleteGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int code = goodsService.deleteGoods(id);
        if(code == 1) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("删除失败")));
    }

    /**
     * 删除商品类型
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void deleteType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int typeId = Integer.parseInt(request.getParameter("typeId"));
        int code = goodsService.deleteType(typeId);
        if(code == 1) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("删除失败")));
    }

    /**
     * 根据商品类型获取商品
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int typeId = Integer.parseInt(request.getParameter("typeId"));
        List<GetGoodsByTypeVO> list = goodsService.getGoodsByType(typeId);
        response.getWriter().println(gson.toJson(Result.ok(list)));
    }

    /**
     * 获取商品类型
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<GoodsTypeVO> list = goodsService.getType();
        response.getWriter().println(gson.toJson(Result.ok(list)));
    }
}
